package org.simple.rpc.redis.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * redis存储对象的时候的 序列化和反序列化工具包
 * @author bo_wang
 *  对象要序列化 必须要实现Serializable 接口 
 */
public class SerializeUtil {
	
	/**
	 * 对象的序列化
	 * @param object
	 * @return
	 */
	public static byte[] serializes(Object object) {
		byte[] bytes=null;
		ObjectOutputStream outputStream=null;
		ByteArrayOutputStream byteArrayOutputStream=null;
		try {
			byteArrayOutputStream=new ByteArrayOutputStream();
			outputStream=new ObjectOutputStream(byteArrayOutputStream);
			outputStream.writeObject(object);
			bytes=byteArrayOutputStream.toByteArray();
		} catch (Exception e) {
			System.out.println("序列化失败");
		}
	   return bytes;
	}
	
	/**
	 * 反序列化
	 * @param bytes
	 * @return
	 */
	public static Object unserializes(byte[] bytes){
		Object object=null;
		ByteArrayInputStream byteArrayInputStream=null;
		try {
			byteArrayInputStream=new ByteArrayInputStream(bytes);
			ObjectInputStream objectInputStream=new ObjectInputStream(byteArrayInputStream);
			object= objectInputStream.readObject();
		} catch (Exception e) {
			System.out.println("反序列化失败");
		}
		return object;
	}
	

}
